package com.xfcy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfcy.blog.common.R;
import com.xfcy.blog.common.constant.RedisConstant;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.common.enums.UploadEnum;
import com.xfcy.blog.mapper.PhotoMapper;
import com.xfcy.blog.entity.Photo;
import com.xfcy.blog.service.PhotoService;
import com.xfcy.blog.service.SystemConfigService;
import com.xfcy.blog.strategy.context.FileUploadStrategyContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 照片(Photo)表服务实现类
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:06
 */
@Service("photoService")
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {

    @Resource
    private FileUploadStrategyContext fileUploadStrategyContext;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 后台相片列表
     *
     * @param pageNo
     * @param pageSize
     * @param albumId
     * @return
     */
    @Override
    public R listPhoto(Integer pageNo, Integer pageSize, Integer albumId) {
        Page<Photo> photoPage = baseMapper.selectPage(new Page<>(pageNo, pageSize), new QueryWrapper<Photo>().eq(albumId != null, SqlConstant.ALBUM_ID, albumId));
        return R.success(photoPage);
    }

    /**
     * 移动其他相册
     *
     * @param map
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R movePhoto(Map<String, Object> map) {
        Integer albumId = (Integer) map.get("albumId");
        List<Integer> ids = (List<Integer>) map.get("ids");
        baseMapper.movePhoto(ids, albumId);
        return R.success();
    }

    /**
     * 查询图片详情
     *
     * @param id
     * @return
     */
    @Override
    public R getPhotoById(Integer id) {
        Photo photo = baseMapper.selectById(id);
        return R.success(photo);
    }

    /**
     * 新增图片
     *
     * @param photo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R insertPhoto(Photo photo) {
        int rows = baseMapper.insert(photo);
        if (rows > 0) {
            redisTemplate.opsForSet().add(RedisConstant.DB_FILE_UPLOAD_IMAGES, photo.getUrl());
            return R.success();
        }
        return R.error("添加照片失败");
    }

    /**
     * 修改图片
     *
     * @param photo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updatePhoto(Photo photo) {
        Photo plainPhoto = baseMapper.selectById(photo.getId());
        int rows = baseMapper.updateById(photo);
        if (!plainPhoto.getUrl().equals(photo.getUrl()) && rows > 0) {
            redisTemplate.opsForSet().remove(RedisConstant.DB_FILE_UPLOAD_IMAGES, plainPhoto.getUrl());
            redisTemplate.opsForSet().add(RedisConstant.DB_FILE_UPLOAD_IMAGES, photo.getUrl());
        }
        return rows > 0 ? R.success() : R.error("修改图片失败");
    }


    /**
     * 批量删除图片
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatchPhoto(List<Integer> ids) {
        int rows = baseMapper.deleteBatchIds(ids);
        if (rows > 0) {
            List<String> urls = baseMapper.selectListByIds(ids);
            redisTemplate.opsForSet().remove(RedisConstant.DB_FILE_UPLOAD_IMAGES, urls.toArray());
        }
        return rows > 0 ? R.success() : R.error("批量删除照片失败");
    }


    // 自定义方法 =======

}


